<?php defined('SYSPATH') or die('No direct script access.');

class Controller_User extends Controller_Page_Template
{
	private $user_id = NULL;
	public function before()
	{
		parent::before();
		$this->user_id = Helper_Auth::instance()->get_user_id();
		
		$this->add_sidebar_view(View::factory('sidebar/profile'));
	}
	public function action_login()
	{
		if($_POST)
		{
			$account  = $_POST['account'];
          	$password = $_POST['password'];
          	
          	if (!$account || !$password)
          	{
            	echo "Enter username and password";
            	exit;
          	}
          	
          	// Check if user exists
          	if (Helper_Auth::instance()->login($account, $password))
          	{
          		// Redirect to user home
          		Request::current()->redirect('/project');
          	}
          		
          	$this->primary_content = View::factory('/partials/login',array('error' => 1));
		} else
			$this->primary_content = View::factory('/partials/login');
		
	}
	public function action_logout()
	{
		Helper_Auth::instance()->logout();
		Request::current()->redirect('/project');
	}
	public function action_index()
	{
		
	}
	public function action_register()
	{
		if ($_POST)
		{
			//echo debug::dump($_POST);		
			$data = $_POST;
			
			$data['birthday']		= $data['year'].'-'.$data['month'].'-'.$data['day'];
			$data['displayname']	= $data['first_name'].' '.$data['last_name'];
			$data['stat_project']	= 0;
			$data['date_create'] 	= $data['date_update'] = DB::expr('NOW()');
			
			unset($data['day']);
			unset($data['month']);
			unset($data['year']);
			unset($data['day']);
			unset($data['save']);
			unset($data['password_confirm']);
			
			$ret = Model::factory('user')->add_user($data);
			$this->request->redirect('/');
		}
		else 
			$this->primary_content = View::factory('/user/register');
	}
	public function action_submit_register()
	{
		$info['displayname'] = $_POST['displayname'];
		$info['email'] = $_POST['email'];
		$info['password'] = $_POST['password'];
		$info['date_create'] = DB::expr('NOW()');
		$info['date_update'] = DB::expr('NOW()');
		$ret = Model::factory('user')->add_user($info);
		if ($ret)
			$this->primary_content = View::factory('/user/register',array('success' => 1));
		else
			$this->primary_content = View::factory('/user/register');			
	}
	public function action_add_organization()
	{
		$this->primary_content = View::factory('/organization/add_organization');
	}
	public function action_submit_add_organization()
	{		
		$info = $_POST;
		unset($info['save']);
		
		$ret = Model::factory('organization')->add_organization($info);
		$ret = Model::factory('organization')->add_user_2_organization( $this->user_id,$ret[0],Helper_MyConst::ORGANIZATION_OWNER);

		if ($ret)
			$this->primary_content = View::factory('/about');
		else
			$this->primary_content = View::factory('/user/register');	
	}
	public function action_profile()
	{
		$tab = isset($_GET['tab']) ? $_GET['tab'] : 'tasks';
		if ($tab == 'tasks')
			$this->primary_content = View::factory('/user/tasks');
		elseif ($tab == 'info')
			$this->primary_content = View::factory('/user/info');
		elseif ($tab == 'milestones')
			$this->primary_content = View::factory('/user/milestones');  
			
	}
	public function after()
	{
		
		parent::after();
	}
}

?>